package com.ruoyi.subject.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.subject.domain.QbSubCollect;
import com.ruoyi.subject.mapper.QbSubCollectMapper;
import com.ruoyi.subject.service.IQbSubCollectService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 我的收藏Service业务层处理
 *
 * @author lzhj
 * @date 2021-01-27
 */
@Service
public class QbSubCollectServiceImpl implements IQbSubCollectService {
    @Autowired
    private QbSubCollectMapper qbSubCollectMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询我的收藏
     *
     * @param id 我的收藏ID
     * @return 我的收藏
     */
    @Override
    public QbSubCollect selectQbSubCollectById(Long id) {
        return qbSubCollectMapper.selectQbSubCollectById(id);
    }

    /**
     * 查询我的收藏列表
     *
     * @param qbSubCollect 我的收藏
     * @return 我的收藏
     */
    @Override
    public List<QbSubCollect> selectQbSubCollectList(QbSubCollect qbSubCollect) {
        return qbSubCollectMapper.selectQbSubCollectList(qbSubCollect);
    }

    /**
     * 新增我的收藏
     *
     * @param qbSubCollect 我的收藏
     * @return 结果
     */
    @Override
    public int insertQbSubCollect(QbSubCollect qbSubCollect) {
        qbSubCollect.setCreateTime(DateUtils.getNowDate());
        return qbSubCollectMapper.insertQbSubCollect(qbSubCollect);
    }

    /**
     * 修改我的收藏
     *
     * @param qbSubCollect 我的收藏
     * @return 结果
     */
    @Override
    public int updateQbSubCollect(QbSubCollect qbSubCollect) {
        qbSubCollect.setUpdateTime(DateUtils.getNowDate());
        return qbSubCollectMapper.updateQbSubCollect(qbSubCollect);
    }

    /**
     * 批量删除我的收藏
     *
     * @param ids 需要删除的我的收藏ID
     * @return 结果
     */
    @Override
    public int deleteQbSubCollectByIds(Long[] ids) {
        return qbSubCollectMapper.deleteQbSubCollectByIds(ids);
    }

    /**
     * 删除我的收藏信息
     *
     * @param id 我的收藏ID
     * @return 结果
     */
    @Override
    public int deleteQbSubCollectById(Long id) {
        return qbSubCollectMapper.deleteQbSubCollectById(id);
    }

    @Override
    public List<QbSubCollect> selectQbSubCollectListByUserId(Long userId) {
        return qbSubCollectMapper.selectQbSubCollectListByUserId(userId);
    }

    @Override
    public Long selectQbSubCollectBy(QbSubCollect qbSubCollect) {
        Long id = null;
        String sql = "select id from t_qb_sub_collect where user_id=? and subject_id=?";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, qbSubCollect.getUserId(), qbSubCollect.getSubjectId());
        if (CollectionUtils.isNotEmpty(list)){
            id = list.get(0).get("id") == null ? null : Long.parseLong(list.get(0).get("id").toString());
        }
        return id;
//        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class, qbSubCollect.getUserId(), qbSubCollect.getSubjectId());
//        return integer > 0 ? true : false;
    }
}
